package com.study.job;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 可通过属性配置 streaming.process 开启或关闭流式处理
 * @author Mayjean
 */
@Slf4j
@Component
public class MyDataFlowJob implements DataflowJob<Object> {

    /**
     * 获取数据
     * @param context
     * @return
     */
    @Override
    public List<Object> fetchData(ShardingContext context) {
        switch (context.getShardingItem()) {
            case 0:
                //List<Object> data = context. // get data from database by sharding item 0
                //return data;
            case 1:
                //List<Object> data = // get data from database by sharding item 1
                //return data;
            case 2:
                //List<Object> data = // get data from database by sharding item 2
                //return data;
                // case n: ...
            default:
                break;
        }
        log.info("fetchData:{}",context);

        List<Object> data = new ArrayList<>();
        data.add("data1");
        data.add("data2");
        data.add("data3");
        return data;
    }

    /**
     * 处理数据
     * @param shardingContext
     * @param data
     */
    @Override
    public void processData(ShardingContext shardingContext, List<Object> data) {
        log.info("processData:{},{}",shardingContext, data);
    }

}